﻿using Application.MainModule.PhonesManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.Phones;
using Domain.MainModule.Entities;
using System.Collections.Generic;

using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for PhoneManagementServiceTest and is intended
    ///to contain all PhoneManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PhoneManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for AddPhone
        ///</summary>
        [TestMethod()]
        public void AddPhone_Invoke_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
            Guid phoneId = Guid.Parse("ABCDF456-1234-5678-5678-ACAAAABBBBBB");
            Phone phone = new Phone()
                        {
                            PhoneId = phoneId,
                            Value = "New Phone",
                        };
            Phone result;

            //Act
            target.AddPhone(phone);

            //Assert
            result = target.FindPhoneById(phoneId);
            Assert.IsNotNull(result);
            Assert.AreEqual(phoneId, result.PhoneId);
            target.RemovePhone(result);
        }

        /// <summary>
        ///A test for ChangePhone
        ///</summary>
        [TestMethod()]
        public void ChangePhone_Invoke_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
            Guid phoneId = Guid.Parse("ABCDF456-1234-5678-5678-FFFFFFBBBBBB");
            Phone phone = null, result = null;

            //Act
            phone = target.FindPhoneById(phoneId);
            phone.Value = "modified value";
            target.ChangePhone(phone);

            //Assert
            result = target.FindPhoneById(phoneId);
            Assert.IsNotNull(result);
            Assert.AreEqual("modified value", result.Value);
        }

        /// <summary>
        ///A test for FindPhoneById
        ///</summary>
        [TestMethod()]
        public void FindPhoneById_Invoke_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
            Guid phoneId = Guid.Parse("ABCDF456-1234-5678-5678-FFFFFFBBBBBB");
            Phone phone = null;

            //Act
            phone = target.FindPhoneById(phoneId);

            //Assert
            Assert.IsNotNull(phone);
            Assert.AreEqual(phoneId, phone.PhoneId);
        }

        /// <summary>
        ///A test for FindPhoneByUserId
        ///</summary>
        [TestMethod()]
        public void FindPhoneByUserId_Invoke_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
            Guid userId = Guid.Parse("02CDF456-1234-5678-5678-AAAAAABBBBBB");
            List<Phone> phones = null;

            //Act
            phones = target.FindPhoneByUserId(userId);

            //Assert
            Assert.IsNotNull(phones);
            Assert.AreEqual(1, phones.Count);
        }

        /// <summary>
        ///A test for FindPhoneTypesByName
        ///</summary>
        [TestMethod()]
        public void FindPhoneTypesByName_Invoke_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
            string phoneTypeName = "Home Phone";
            List<PhoneType> phoneTypes = null;

            //Act
            phoneTypes = target.FindPhoneTypesByName(phoneTypeName);

            //Assert
            Assert.IsNotNull(phoneTypes);
            Assert.AreEqual(1, phoneTypes.Count);
            Assert.AreEqual(phoneTypeName, phoneTypes[0].Name);
        }

        /// <summary>
        ///A test for RemovePhone with null phone throw argument null exception.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemovePhone_NullPhoneThrowArgumentNullException_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();

            //Act
            target.AddPhone(null);
        }

        /// <summary>
        ///A test for RemovePhone
        ///</summary>
        [TestMethod()]
        public void RemovePhone_Invoke_Test()
        {
            //Arrange
            IPhoneManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
            Guid phoneId = Guid.Parse("ABCDF456-1234-5678-5678-ACAAAABBBBBB");
            Phone phone = new Phone()
            {
                PhoneId = phoneId,
                Value = "New Phone"
            };
            Phone result;

            //Act
            target.AddPhone(phone);
            target.RemovePhone(phone);

            //Assert
            result = target.FindPhoneById(phoneId);
            Assert.IsNull(result);
        }
    }
}
